#include "SetModbusDialog.h"
#include <QMessageBox>

SetModbusDialog::SetModbusDialog()
{
    _UIInit();
}

SetModbusDialog::~SetModbusDialog()
{

}

void SetModbusDialog::sl_u_buttonBox_clicked(QAbstractButton *button)
{
    if( dynamic_cast<QAbstractButton*>(u_buttonBox->button(QDialogButtonBox::StandardButton::Ok)) == button )
    {
        bool ok;
        int serverAddr = u_serverAddrLineEdit->text().toInt(&ok);
        if(ok == false){
           QMessageBox::about(this, "", "从机地址应为数字");
           return;
        }

        int readStartAddr = u_readStartAddrLineEdit->text().toInt(&ok);
        if(ok == false){
           QMessageBox::about(this, "", "开始地址应为数字");
           return;
        }
        if(readStartAddr<0 || readStartAddr>124){
            QMessageBox::about(this, "", "读取长度不得小于0大于124");
            return;
        }

        g_modbusSetting.registerType = (QModbusDataUnit::RegisterType)(u_registerTypeComboBox->currentData().toInt());
        g_modbusSetting.serverAddr = serverAddr;
        g_modbusSetting.readStartAddr = readStartAddr;
        g_modbusSetting.readType = (ModbusSetting::TypeEnum)u_readTypeComboBox->currentIndex();
        if(g_modbusSetting.readType == ModbusSetting::Signed)
            g_modbusSetting.readValueCount = Signed_Size;
        else if(g_modbusSetting.readType == ModbusSetting::Unsidned)
            g_modbusSetting.readValueCount = Unsigned_Size;
        else if(g_modbusSetting.readType == ModbusSetting::Long)
            g_modbusSetting.readValueCount = Long_Size;
        else if(g_modbusSetting.readType == ModbusSetting::Float)
            g_modbusSetting.readValueCount = Float_Size;
        else if(g_modbusSetting.readType == ModbusSetting::Double)
            g_modbusSetting.readValueCount = Double_Size;
        else
            g_modbusSetting.readValueCount = Signed_Size;
        accept();
    }
    else
    {
        _FillUI();
        hide();
    }
}

void SetModbusDialog::_UIInit()
{
    u_layout = new QVBoxLayout(this);

    QHBoxLayout* registerTypeLayout = new QHBoxLayout();
    QLabel* registerTypeLabel = new QLabel("功能码", this);
    u_registerTypeComboBox = new QComboBox(this);
    u_registerTypeComboBox->addItem("DiscreteInputs", QModbusDataUnit::RegisterType::DiscreteInputs);
    u_registerTypeComboBox->addItem("Coils", QModbusDataUnit::RegisterType::Coils);
    u_registerTypeComboBox->addItem("InputRegisters", QModbusDataUnit::RegisterType::InputRegisters);
    u_registerTypeComboBox->addItem("HoldingRegisters", QModbusDataUnit::RegisterType::HoldingRegisters);
    registerTypeLayout->addWidget(registerTypeLabel);
    registerTypeLayout->addWidget(u_registerTypeComboBox);
    u_layout->addLayout(registerTypeLayout);

    QHBoxLayout* serverAddrLayout = new QHBoxLayout();
    QLabel* serverAddrLabel = new QLabel("设备地址", this);
    u_serverAddrLineEdit = new QLineEdit(this);
    serverAddrLayout->addWidget(serverAddrLabel);
    serverAddrLayout->addWidget(u_serverAddrLineEdit);
    u_layout->addLayout(serverAddrLayout);

    QHBoxLayout* readStartAddrLayout = new QHBoxLayout();
    QLabel* readStartAddrLabel = new QLabel("开始地址", this);
    u_readStartAddrLineEdit = new QLineEdit(this);
    readStartAddrLayout->addWidget(readStartAddrLabel);
    readStartAddrLayout->addWidget(u_readStartAddrLineEdit);
    u_layout->addLayout(readStartAddrLayout);

    QHBoxLayout* readTypeLayout = new QHBoxLayout();
    QLabel* readTypeLabel = new QLabel("读取类型", this);
    u_readTypeComboBox = new QComboBox(this);
    u_readTypeComboBox->addItem("Signed");
    u_readTypeComboBox->addItem("Unsidned");
    u_readTypeComboBox->addItem("Long");
    u_readTypeComboBox->addItem("Float");
    u_readTypeComboBox->addItem("Double");
    readTypeLayout->addWidget(readTypeLabel);
    readTypeLayout->addWidget(u_readTypeComboBox);
    u_layout->addLayout(readTypeLayout);

    u_buttonBox = new QDialogButtonBox(QDialogButtonBox::StandardButton::Ok|QDialogButtonBox::StandardButton::Cancel
                                       ,this);
    connect(u_buttonBox, &QDialogButtonBox::clicked, this, &SetModbusDialog::sl_u_buttonBox_clicked);
    u_layout->addWidget(u_buttonBox);

    _FillUI();
}

void SetModbusDialog::_FillUI()
{
    if(QModbusDataUnit::RegisterType::DiscreteInputs == g_modbusSetting.registerType){
        u_registerTypeComboBox->setCurrentIndex(0);
    }
    else if(QModbusDataUnit::RegisterType::Coils == g_modbusSetting.registerType){
        u_registerTypeComboBox->setCurrentIndex(1);
    }
    else if(QModbusDataUnit::RegisterType::InputRegisters == g_modbusSetting.registerType){
        u_registerTypeComboBox->setCurrentIndex(2);
    }
    else if(QModbusDataUnit::RegisterType::HoldingRegisters == g_modbusSetting.registerType){
        u_registerTypeComboBox->setCurrentIndex(3);
    }

    u_serverAddrLineEdit->setText(QString::number(g_modbusSetting.serverAddr));
    u_readStartAddrLineEdit->setText(QString::number(g_modbusSetting.readStartAddr));
    u_readTypeComboBox->setCurrentIndex(g_modbusSetting.readType);
}
